package com.dongdong.android.framework.download;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DownloadThreadPool {
    public static String TAG = "DownloadThreadPool";
    private static int corePoolSize = 5;
    private static int maxPoolSize = 5;
    private ThreadPoolExecutor mExecutor;
    private int mActiveCount = 0;

    private DownloadThreadPool() {

        long keepAliveTime = 3000;
        TimeUnit unit = TimeUnit.MILLISECONDS;
        BlockingQueue workQueue = new LinkedBlockingDeque<>();
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();

        mExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, unit, workQueue,
                threadFactory, handler);

    }

    public static class DownloadThreadPoolHolder {
        public final static DownloadThreadPool downloadThreadPool = new DownloadThreadPool();
    }

    public static DownloadThreadPool getInstance() {
        return DownloadThreadPoolHolder.downloadThreadPool;
    }

    public void execute(Runnable task) {

        mExecutor.execute(task);
    }

    public boolean remove(Runnable task) {

        boolean isRemove = mExecutor.remove(task);
        return isRemove;
    }

    public int getActiveCount() {
        return mExecutor.getActiveCount();
    }


}
